// 表单提交工具函数
function handleFormSubmit(formId, successId, errorId) {
    const form = document.getElementById(formId);
    const successMessage = document.getElementById(successId);
    const errorMessage = document.getElementById(errorId);

    if (!form || !successMessage || !errorMessage) {
        console.error('Form elements not found');
        return;
    }

    form.addEventListener('submit', function(e) {
        e.preventDefault();
        const submitButton = form.querySelector('button[type="submit"]');
        const originalText = submitButton ? submitButton.textContent : '';

        // 显示加载状态
        if (submitButton) {
            submitButton.disabled = true;
            submitButton.textContent = '提交中...';
        }

        try {
            // 模拟表单提交延迟
            setTimeout(() => {
                successMessage.style.display = 'block';
                errorMessage.style.display = 'none';
                form.reset();

                // 5秒后隐藏成功消息
                setTimeout(() => {
                    successMessage.style.display = 'none';
                }, 5000);

                // 恢复按钮状态
                if (submitButton) {
                    submitButton.disabled = false;
                    submitButton.textContent = originalText;
                }
            }, 1000);
        } catch (error) {
            successMessage.style.display = 'none';
            errorMessage.style.display = 'block';
            errorMessage.textContent = '提交失败，请稍后重试';

            // 恢复按钮状态
            if (submitButton) {
                submitButton.disabled = false;
                submitButton.textContent = originalText;
            }
            console.error('Form submission error:', error);
        }
    });
}

// 暴露到全局供页面调用
window.handleFormSubmit = handleFormSubmit;